﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

public enum Interact
{
    DIALOG, INTERACTION
}

public class ThoughtScreen : GameScreen
{
    Interact interact;
    SpriteFont font;
    Texture2D texture;
    string sentence;
    List<Thought> thoughtList;
    int thoughtNum;

    Thought thought;

    public ThoughtScreen(Thought thought, Interact interact)
        : base()
    {
        this.interact = interact;
        IsPopup = true;
        Globals.IsPaused = true;
        this.sentence = thought.Sentence;

        this.thought = thought;

        if (!thought.Sound.Equals(string.Empty))
            AudioManager.PlaySound(thought.Sound);
    }

    public override void LoadContent()
    {
        texture = GameSession.Content.Load<Texture2D>("Textures/pensamento");
        font = GameSession.Content.Load<SpriteFont>("SpriteFont1");

        base.LoadContent();
    }

    public override void Draw(GameTime gameTime)
    {
        SpriteBatch spriteBatch = ScreenManager.SpriteBatch;

        spriteBatch.Begin();

        spriteBatch.Draw(texture, new Vector2(500, 0), Color.White);

        WriteString(spriteBatch, sentence, 70);

        //spriteBatch.DrawString(font, sentence, new Vector2(580, 70), Color.Black);

        spriteBatch.End();

        base.Draw(gameTime);

    }

    private int WriteString(SpriteBatch spriteBatch, string text, int y)
    {
        string[] words = text.Split(' ');

        string aux = "";

        int draws = 0;

        for (int i = 0; i < words.Length; i++)
        {
            if (aux.Equals(""))
                aux += words[i];
            else
                aux += " " + words[i];

            //if (aux.Length > 140)
             if (font.MeasureString(aux).X > 400)
            {
                spriteBatch.DrawString(font, aux, new Vector2(580, y + draws * 25), Color.Black);
                draws++;
                aux = "";
            }
        }
        spriteBatch.DrawString(font, aux, new Vector2(580, y + draws * 25), Color.Black);
        draws++;

        return draws;
    }

    public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
    {
        base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);

        if (Globals.FirstThought)
        {
            ScreenManager.AddScreen(new TutorialScreen(TutorialScreen.TutorialKind.THOUGHT));
            Globals.FirstThought = false;
        }
    }

    public override void HandleInput()
    {
        if (InputManager.IsActionTriggered(InputManager.Action.Back))
        {
            Globals.IsPaused = false;
            if (!thought.Sound.Equals(string.Empty))
                AudioManager.StopSound();
            ExitScreen();
        }

        if (InputManager.IsActionTriggered(InputManager.Action.Space))
        {
            Exit();
        }

        base.HandleInput();
    }

    private void Exit()
    {
        if (!thought.Sound.Equals(string.Empty))
            AudioManager.StopSound();

        Globals.IsPaused = false;
        ExitScreen();

        if (interact == Interact.INTERACTION)
            GameSession.Player.StartInteraction();
        if(interact == Interact.DIALOG)
            GameSession.Player.StartDialog();

    }

}
